package BanyuanClub.Study.Day0309.锁相关1;

public class Dog extends Thread{
    private GuanTou guanTou;
    public Dog (GuanTou guanTou){
        this.guanTou = guanTou;
    }

    @Override
    public void run() {
        synchronized (guanTou) {
            if (guanTou.rou > 0) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                guanTou.rou -= 1;
                System.out.println(this + "吃了块肉");
            }
            System.out.println("还剩" + guanTou.rou + "块肉");
        }
    }
    public static void main(String[] args) {
        GuanTou guantou = new GuanTou();
        Dog d1 = new Dog(guantou);
        Dog d2 = new Dog(guantou);
        d1.start();
        d2.start();
    }
}
